<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>供应商类型</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <script src="../../../../res/js/jquery-3.4.1.min.js" charset="utf-8"></script>
    <script src="../../../../res/js/login_common.js" charset="utf-8"></script>
    <script src="../../../../res/layui/layui.js" charset="utf-8"></script>
    <script src="../../../../res/js/Tools.js"></script>
    <link rel="stylesheet" href="../../../../res/layui/css/layui.css" media="all">
</head>

<body>
<form class="layui-form layui-form-pane" id="formDId" action="" method="post" enctype="multipart/form-data">
    <input type="hidden" name="pkSupplierTypeId" id ="pkSupplierTypeId" />

    <div class="layui-form-item">
        <label class="layui-form-label">上级类型</label>
        <div class="layui-input-block">
            <div id="supplierSuperior" class="xm-select-demo"></div>
        </div>
        <span id="msg">不选则为一级菜单</span>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label" style="width:150px">供应商类型名称</label>
        <div class="layui-input-block" style="margin-left: 150px">
            <input type="text" name="supplierTypeName" id = "supplierTypeName" autocomplete="off" lay-verify="required" placeholder="请输入供应商类型名称"
                   class="layui-input">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block" id="flows">
            <button type="submit" class="layui-btn site-demo-active" lay-submit="" lay-filter="demo1">提交</button>
            <input type="button" id="demo3" class="layui-btn layui-btn-primary" value="取消">
        </div>
    </div>
</form>
<script>
    layui.use(['form', 'laydate','upload'], function () {
        var form = layui.form
            , laydate = layui.laydate;
        var $ = layui.jquery
            ,upload = layui.upload;

        var pkSupplierTypeId =getQueryString("pkSupplierTypeId");
        var show = getQueryString("show");
        if(pkSupplierTypeId){
            findById(pkSupplierTypeId);
        }


        //加载组件
        layui.config({
            //引入扩展文件库
            base: getRootPath()+'res/layui/dist/'
        }).extend({
            //引入对应的文件名
            xmSelect: 'xm-select'
            //拿到对应的文件模块
        }).use(['xmSelect'], function(){
            //对于模块赋值使用
            var xmSelect = layui.xmSelect;
            //渲染多选
            pkJurisdictionId = xmSelect.render({
                el: '#supplierSuperior',
                radio: true,
                name:'supplierSuperior',
                tree: {
                    show: true,
                    strict: false,
                    expandedKeys: [ -1 ],
                },
                height: 'auto',
                data: loadOaSupplierType(pkSupplierTypeId,show)
            })

        });
        function loadOaSupplierType(id,show){
            var url = getRootPath()+"OaSupplierTypeAction/loadOaSupplierType";
            var data = {
                id : id, //需要操作的数据
                show:'1' // 判断是否是查看
            };
            var res = getAjax(url,data);
            if(res){
                return res;
            }else{
                return [];
            }

        }


        var res;
        if (pkSupplierTypeId) {
            res = getAjaxById(getRootPath() + "OaSupplierTypeAction/findById/" + pkSupplierTypeId);
            //客户类型id
            $("#pkSupplierTypeId").val(res.data.pkSupplierTypeId);
            //客户类型
            var supplierTypeName = $("#supplierTypeName");
            supplierTypeName.val(res.data.supplierTypeName);

            //判断是否是查看
            if (show) {
                //工作总结类型
                formDisabled($("#pkSupplierTypeId"));
                //本阶段总结
                formDisabled(supplierTypeName);
                formDisabled($("#supplierTypeSuperior"));
                console.info("隐藏按钮")
                //隐藏提交按钮
                $("#sub").hide();

            }
            form.render();
        }
        form.render();


        //监听起草提交
        form.on('submit(demo1)', function (data) {

            console.info(data.field);
            // 单击之后提交按钮不可选,防止重复提交
            var DISABLED = 'layui-btn-disabled';
            // 增加样式
            $('.site-demo-active').addClass(DISABLED);
            // 增加属性
            $('.site-demo-active').attr('disabled', 'disabled');
            var formData = new FormData(document.getElementById("formDId"));
            console.log(formData)
            var res = postAjax(getRootPath() + "OaSupplierTypeAction/updateOrAdd", data.field);
            if (res.code === 1) {
                layer.msg('新增成功3秒后关闭当前窗口', {icon: 1});
                setTimeout(function () {
                    formClose();
                }, 3000);
                setTimeout(function () {
                    parent.layui.table.reload('test');//刷新父页面
                    //window.parent.location.reload();//刷新父页面
                    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                    parent.layer.close(index);
                }, 3000);
                //window.location.reload();//刷新页面
            } else if (res.code === 500) {
                error('500');
            } else {
                layer.msg("新增失败");
            }
            return false;
        });

        $("#demo3").on("click", function () {
            //询问框
            layer.confirm('是否关闭该页面？', {
                btn: ['是的', '取消'] //按钮
            }, function () {
                formClose();
                parent.layui.table.reload('test');//刷新父页面
                //window.parent.location.reload();//刷新父页面
                var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                parent.layer.close(index);
            });
        });
    });
    /**
     * 禁用
     *  @param forms 控件
     */
    function formDisabled(forms) {
        var DISABLED = 'layui-btn-disabled';
        forms.attr('disabled', 'disabled');
        forms.addClass(DISABLED);
    }
    function findById(id) {
        var url=getRootPath()+"OaSupplierTypeAction/findById/"+id;
        var data=getAjaxById(url);
        console.log(data.data.supplierTypeName+"上级名称");
        $("#pkSupplierTypeId").val(data.data.pkSupplierTypeId);
        $("#supplierTypeName").val(data.data.supplierTypeName);
    }

</script>

</body>
</html>